package com.yunhe.day322.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.day322.domain.Product;
import com.yunhe.day322.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/product")
public class productController {
    //产品业务层
    @Autowired
    private ProductService productService;

    /**
     * 添加产品
     *
     * @param product
     * @return
     */
    @RequestMapping("/save")
    public String addProduct(Product product) {

        productService.addProduct(product);
        return "redirect:findAll";
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
    public String showAllProduct(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "sezr", defaultValue = "5") int size, Model model) {
        PageHelper.startPage(page, size);
        List<Product> list = productService.findAllProduct(page, size);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo", pageInfo);
        return "product-list";
    }
    /**
     * 修改产品
     * @return
     */
    @PostMapping("edit")
    public String edit( @RequestParam("id") String id,
                        @RequestParam("productNum") String productNum,
                        @RequestParam("productName") String productName,
                        @RequestParam("departureTime") String departureTime,
                        @RequestParam("cityName") String cityName,
                        @RequestParam("productPrice") Integer productPrice,
                        @RequestParam("productStatus") String productStatus,
                        @RequestParam("productDesc") String productDesc){

            Product product = new Product();
            product.setId(id);
            product.setProductNum(productNum);
            product.setProductName(productName);
            product.setDepartureTimeStr(departureTime);
            product.setCityName(cityName);
            product.setProductPrice(productPrice);
            product.setProductStatusStr(productStatus);
            product.setProductDesc(productDesc);

            this.productService.edit(product);

            return "redirect:findAll";


    }
    /**
     * 修改之前进行查询
     * @param id
     * @param model
     * @return
     */
    @GetMapping("editById")
    public String editById( String id , Model model){
        Product product =  this.productService.findById(id);
        model.addAttribute("product",product);
        return "product-edit";
    }
    /**
     * 产品详情
     * @param id
     * @param model
     * @return
     */
    @GetMapping("findById")
    public String findById( String id, Model model){
        Product product = this.productService.findById(id);
        model.addAttribute("product",product);
        return "product-show";
    }
    /**
     * 指定删除
     * @param id
     * @return
     *
     */
    @RequestMapping("deleteById")
    public String deleteById(String id){
        productService.deleteById(id);
       return "redirect:findAll";
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("selectDelete")
    public String selectDelete( String[] ids){
        for (String id : ids) {
            productService.deleteById(id);
        }
        return "redirect:findAll";
    }
}
